//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    tabbarIndex:'',
    indicatorDots: true,
    city:'',
    city1:'',
    autoplay: true,
    interval: 5000,
    duration: 500,
    logo: true,
    navindex:0,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    buytype:false,
    hidden:true,
    scrollHeight: 0,
    pageNum: 1,
    car_length:'',
    car_type:'',
    buyindex:0,
    xingindex:0,
    chose_c:true,
    movies: [
       
    ],
    nav:[
      { title: '汽配商城', pic: '/images/nav_01.png', url: '../product/product' },
      { title: '我的车辆', pic: '/images/nav_02.png', url: '../options/options' },
      { title: '订单中心', pic: '/images/nav_03.png', url: '../waybill/waybill' },
      { title: '充值', pic: '/images/nav_04.png',url:'../recharge/recharge'},
    ],
    
    //
    freight:[
 
    ],
    freightnav:[
      { txt: '全部' },
      { txt: '常跑路线' },
    ],
    alltype:[
      {
        car_length:[
 
        ],
        car_type: [
 
        ],
      }
    ]
      
  }, 
  pullUpLoad(){
    var base = getApp().data.base_url;
    this.getList(base)
    console.log('--------')
  },
  // 选择地址
  chose_city: function (e) {
    var type = e.currentTarget.dataset.type;
    var that = this;    
    wx.navigateTo({
      url: '../address/address?type=' + type,
    })
  },
  navbin(e){
    var url = e.currentTarget.dataset.url;
    app.navTo(url)
    app.swtTa(url)
  },
  typebuy: function () {
    this.setData({
      buytype: !this.data.buytype
    })
    var base = getApp().data.base_url;
    this.getBanner(base)
  },
  //详情页
  start_roaddeil(e){
    var id = e.currentTarget.dataset.id;
    console.log(id)
    app.navTo('../roaddeil/roaddeil?id'+id)
  },
  // 车长选择
  chose_baby(e) {
    var index = e.currentTarget.dataset.index;
    var that = this;
    var car_length = this.data.car_length;
    this.setData({
      buyindex: index
    })
    var base = getApp().data.base_url;
    this.getList(base)
  },
  chose_buyx: function (e) {
    var index = e.currentTarget.dataset.index;
    var buyx = this.data.choseC;
    this.setData({
      xingindex: index
    })
    var base = getApp().data.base_url;
    this.getList(base)
  },
  click: function (e) {
    var id = e.currentTarget.dataset.id;
    if(id==0){
      this.setData({
        chose_c: true,
        chose_p: false
      })

    }else{
      this.setData({
        chose_p: true,
        chose_c: false
      })

    }
    console.log(id)
    this.setData({
      navindex: id
    })
    // wx.request({
    //   url: '',
    //   data: {
    //   },
    //   success: function (res) {
    //     that.setData({
    //       list: res.data.data
    //     })
    //   }
    // })
  },
  changecity: function (cityName, type){
    if(type == 0){
      console.log("出发城市"+cityName)
      this.setData({
        city: cityName
      })
      var base = getApp().data.base_url;
      this.getList(base)
    }
    else if (type == 1) {
      console.log("送货城市" + cityName)
      this.setData({
        city1: cityName
      })
      var base = getApp().data.base_url;
      this.getList(base)
    }
  },
  getBanner(base){
    var that = this;
    var car_length = that.data.car_length;
    var car_type = that.data.car_type;
    // 获取banner数据 
    wx.request({
      url: base+'api/get_banner', //仅为示例，并非真实的接口地址
      data: {
          
      },
      success: function (res) {
        console.log(res)
        // console.log(res.data)
        that.setData({
          movies: res.data.data
        })
      }
    })
    // 获取nav数据 
    // wx.request({
    //   url: base+'api/get_class', //仅为示例，并非真实的接口地址
    //   data: {

    //   },
    //   success: function (res) {
    //     console.log(res.data)
    //     that.setData({
    //       nav: res.data.data
    //     })
    //   }
    // })
    // 获取车长类型数据 
    wx.request({
      url: base + 'api/get_car_type', //仅为示例，并非真实的接口地址
      data: {

      },
      success: function (res) {
        console.log(res)
        that.setData({
          // alltype: res.data.data.data,
          car_type: res.data.data.car_type,
          car_length: res.data.data.car_length,
        })
      }
    })
  },
  getList(base){
    var that = this; 
    var page_num = that.data.pageNum
    var freight = that.data.freight
    let start_city = this.data.city == "请选择" ? "" : this.data.city;
    let end_city = this.data.city1 == "请选择" ? "" : this.data.city1;
    wx.showLoading()
    wx.request({
      url: base + 'api/get_task_list', //仅为示例，并非真实的接口地址
      data: {
        page_num: page_num,
        start_city,
        end_city
      },
      success: function (res) {
        wx.hideLoading()
        if (page_num == 1) { //第一次加载（onload事件）
          that.setData({
            freight: res.data.data,
            pageNum: page_num + 1
          })
        } else { //上划加载（pullUpLoad事件）
          if (res.data.data.length == 0){
            wx.showToast({
              title: '没有更多了',
              icon: 'loading',
              duration: 1000
            })
          }
          for (var i = 0; i < res.data.data.length; i++) {
            freight.push(res.data.data[i]);
          }
          that.setData({
            freight: freight,
            pageNum: page_num + 1
          })
        }
      }
    })
  },
  //登录
  sendRequest: function (res) {
    var that = this;
    wx.request({
      url: ' ',
      data: {

      },
      method: ' ',
      success: function (res) {

      }
    })
  },
  onLoad: function (res) {
    var base = getApp().data.base_url;
    //显示自定义的底部导航
    if (res.id == 1) {
      this.setData({
        tabbarIndex: res.id
      })
      getApp().editTabBar1();
    } else {
      getApp().editTabBar();
    }
    //banner
    this.getBanner(base);
    this.getList(base);
    // 倒计时
    var freight = this.data.freight;
    let that = this;    
    for (let i = 0; i < freight.length; i++) {
      freight[i].hour = freight[i].count.split(":")[0]
      freight[i].minute = freight[i].count.split(":")[1]
      freight[i].second = freight[i].count.split(":")[2]
    }
    if (!app.data.address.name) {
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          console.log(res);
        },
      })
    }
    this.setData({
      freight,
      city: app.data.address.name || "请选择",
      city1: app.data.address.name || "请选择",
    }) 
    wx.getSystemInfo({
    success: function(res) {
        that.setData({
          scrollHeight: res.windowHeight
      });
    }
    });
    // loadMore(that);
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {

  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
